preserve 

*****************************
* CLASS X across all students
*****************************
keep id_school treatment school_tamil
duplicates drop
tempfile treat 
save `treat', replace 

import excel "$data/classX.xlsx", clear firstrow

foreach var of varlist tamil-final {
	gen temp = real(`var')
	drop `var'
	rename temp `var'
	replace `var' = 0 if missing(`var') 
	rename `var' ssc_`var'
}

merge m:1 id_school using `treat', nogen
order treatment, after(id_student)

*Number of boys and girls 
gen female = sex == "F"
gen male = sex == "M"

egen numgirls = sum(female), by(id_school)
egen numboys = sum(male), by(id_school)

foreach var of varlist ssc_* {
	*standardizing the distribution
	qui summ `var' if treatment == 0
	gen z`var' = (`var' - r(mean))/r(sd)
	gen pc_`var' = `var' 
}
replace pc_ssc_final = ssc_final/500 *100
replace community = "UNKOWN" if missing(community)
gen obc = regexm(community ,"bc") | regexm(community ,"oc")

*-------------------------
*PANEL A: Without any controls
*-------------------------

* Set 1: Standardized Test Scores
est clear
foreach var in $xthmarks {

	di "`var' without controls"
	*1) Without any controls
	*local var "time_study_out"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `var' treatment, vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	qui eststo panela_`var': bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reg `var' treatment, vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `var' if treatment == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)
	
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}

		
*-------------------------
*PANEL B: With controls
*-------------------------

* Set 1: Standardized Test Scores
foreach var in $xthmarks {

	di "`var' with controls"
	*1) Without any controls
	*local var "time_study_out"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reghdfe `var' treatment $schcontrols, absorb(sex community) vce(robust)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	qui eststo panelb_`var': bootstrap, seed(2905) cl(id_school) reps($reps) nodots: qui reghdfe `var' treatment $schcontrols, absorb(sex community) vce(robust)
	*eststo: reg `var' treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `var' if treatment == 0
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd local regtype = "OLS", replace
	qui estadd scalar rsq = e(r2)
	
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}


# delimit ;
esttab  panela* using "$tables/TableA3.tex", replace
keep(treatment)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Tamil" "English" "Maths" "Science" "Social Science" "Total", pattern(1 1 1 1 1 1) ///
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
nomtitles
posthead("\midrule \multicolumn{7}{l}{\textit{\textbf{Panel A. Without individual and school controls}}} \\ \midrule")
postfoot("")
stats(pvalreg pvalri rsq, labels("p-val (OLS)" "p-val (RI)" "\midrule R2") ///
fmt(2 2 2)) ///
collabels(none) label booktabs nonotes
;

# delimit ;
esttab  panelb* using "$tables/TableA3.tex", append
keep(treatment) ///
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par)) /// 
starlevels(* .10 ** .05 *** .01) ///
prehead("") ///
posthead("\midrule \multicolumn{7}{l}{\textit{\textbf{Panel B. With individual and school controls}}} \\ \midrule")
stats(pvalreg pvalri mean rsq N, labels("p-val (OLS)" "p-val (RI)" "\midrule Control mean" "\midrule R2" "N") ///
fmt(2 2 2 2 0)) ///
collabels(none) mlabels(none) nonumbers label booktabs nonotes nomtitles;
#delimit cr	


restore 